package com.example.demo.service.Impl;

import com.example.demo.common.utills.CommonResult;
import com.example.demo.common.utills.TimeUtil;
import com.example.demo.entity.Macro;
import com.example.demo.entity.MouthData;
import com.example.demo.entity.MouthDataDto;
import com.example.demo.entity.MouthDataDto2;
import com.example.demo.mapper.MacroMapper;
import com.example.demo.service.MacroService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.Mac;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MacroServiceImpl implements MacroService {
    @Resource
    MacroMapper macroMapper;

    @Override
    public CommonResult selectAllMacro(int pageNum,int pageSize,String name,int id) {
        PageHelper.startPage(pageNum, pageSize);
        //查询产业下的数据
        List<Macro> macros = macroMapper.selectAllMacro(name, id);
        List<Macro> macroList = new ArrayList<>();
        //给产业下的子级赋值
        for (Macro macro : macros) {
            int macro_id = macro.getMacro_id();
            //设置子级
            List<MouthDataDto2> mouthDataDtoList = macroMapper.selectNewMouthData(name,macro_id);
            List<MouthDataDto2>  ls=new ArrayList<>();
            double jz = 0.0;
            for (MouthDataDto2 mouthDataDto2 : mouthDataDtoList) {
                String s = mouthDataDto2.getData();
                String sum=mouthDataDto2.getSumData();
                double sumData = Double.parseDouble(sum);
                jz=sumData/12;
                String[] datas = s.split(",");
                String[] newDataS = new String[12];
                for (int i = 0; i < datas.length; i++) {
                    double data = Double.parseDouble(datas[i]);
                    double newData = (data - jz) / jz;
//                    BigDecimal b = new BigDecimal(newData);
//                    newData = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    if (i != datas.length - 1) {
                        newDataS[i] = newData + ",";
                    }
                    newDataS[i] = newData + "";
                }
                String data = Arrays.toString(newDataS);
                String re = data.replace("[", "");
                String replace = re.replace("]", "");
                mouthDataDto2.setData(replace);
                ls.add(mouthDataDto2);
            }
            macro.setChildren(ls);
            macroList.add(macro);
        }
        PageInfo<List<Macro>> pageInfo = new PageInfo(macros);
        if (pageInfo.getSize() == 0) {
            return new CommonResult(500, "无数据", pageInfo);
        }
        return new CommonResult(200, "查询成功", pageInfo);
    }

    @Override
    public CommonResult selectMouthData(int pageNum,int pageSize,int Fid,int year) {
        PageHelper.startPage(pageNum, pageSize);
        List<MouthData> mouthData = macroMapper.selectMouthData(Fid,year);
        PageInfo<List<MouthData>> pageInfo=new PageInfo(mouthData);
        if(pageInfo.getSize()==0){
            return new CommonResult(500,"无数据",pageInfo);
        }
        return new CommonResult(200,"查询成功",pageInfo);
    }

    @Override
    public CommonResult updateMouthData(MouthData mouthData) {
        String time= TimeUtil.SimpleTime();
        mouthData.setLastUpTime(time);
        String mouths=mouthData.getData();
        String[] mouth=mouths.split(",");
        double sum = 0.0;
        for (int i = 0; i <mouth.length ; i++) {
            double mouthD=Double.parseDouble(mouth[i]);
            sum+=mouthD;
        }
        BigDecimal b = new BigDecimal(sum);
        sum = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        mouthData.setSumData(sum);
        int i = macroMapper.updateMouthData(mouthData);
        if(i!=0){
            return new CommonResult(200,"修改成功");
        }
        return new CommonResult(500,"修改失败");
    }

    @Override
    public CommonResult addMouthData(int Fid,int year,int uid) {
        List<MouthData> list = macroMapper.selectMouthData(Fid, year);
        if(list.isEmpty()){
            String time=TimeUtil.SimpleTime();
            int i = macroMapper.addMouthData(Fid, year,time,uid);
            if(i!=0){
                return new CommonResult(200,"新增成功");
            }
            return new CommonResult(500,"新增失败");
        }
        return new CommonResult(500,"该年份已经存在");
    }

//    @Override
//    public CommonResult resultBFB(int pageNum,int pageSize,String name,int id) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<MouthDataDto> list = macroMapper.selectNewMouthData(name,id);
//        double jz=10.00;
//        for(MouthDataDto mouthDataDto:list){
//            List<MouthDataDto2> children = mouthDataDto.getChildren();
//            for(MouthDataDto2 mouthDataDto2:children){
//                String s = mouthDataDto2.getData();
//                String[] datas=s.split(",");
//                String[] newDataS=new String[12];
//                for (int i = 0; i <datas.length ; i++) {
//                    double data=Double.parseDouble(datas[i]);
//                    double newData=(data-jz)/jz;
//                    BigDecimal b = new BigDecimal(newData);
//                    newData = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//                    if(i!=datas.length-1){
//                        newDataS[i]=newData+",";
//                    }
//                    newDataS[i]=newData+"";
//                }
//                String data= Arrays.toString(newDataS);
//                String re=data.replace("[","");
//                String replace = re.replace("]", "");
//                mouthDataDto2.setData(replace);
//            }
//        }
//        PageInfo<List<MouthDataDto>> pageInfo=new PageInfo(list);
//        return new CommonResult(200,"查询成功",pageInfo);
//    }

}
